﻿Imports System.Data.OleDb

Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try

        
        listViewGetData()

        If (Not Page.IsPostBack) Then
            If (User.Identity.Name <> Nothing) Then


                Dim getUserName As ProfilesManagement = New ProfilesManagement()


                Dim userId As Int32 = New aspnet_UsersManagement().SelectUserIDByUsername(User.Identity.Name)

                Dim dr As OleDbDataReader = getUserName.SelectProfileByUserID(userId)
                Dim name As String = ""
                Dim surname As String = ""

                If dr.HasRows Then
                    dr.Read()
                    If Not IsDBNull(dr("ProfileName")) Then name = (dr("ProfileName"))
                    If Not IsDBNull(dr("ProfileSurname")) Then surname = (dr("ProfileSurname"))
                End If

                lblWelcome.Text = "Welcome " + name + " " + surname
            Else
                lblWelcome.Text = "Welcome guest"
            End If
        End If
        Catch ex As Exception

        End Try

    End Sub

    Private Sub listViewGetData()
        Try


            If (drpSortBy.SelectedValue = 0) And (drpTags.SelectedValue = -1) Then

                Dim myLatestQuizes As QuizManagement = New QuizManagement()
                Dim dr As OleDbDataReader = myLatestQuizes.SelectQuizzesLatest()

                Dim dt As DataTable = New DataTable()
                dt.Load(dr)

                lstQuizzes.DataSource = dt
                lstQuizzes.DataBind()

            ElseIf (drpSortBy.SelectedValue = 1) And (drpTags.SelectedValue = -1) Then
                Dim myLatestHighestRating As Quiz_RatingManagement = New Quiz_RatingManagement()
                Dim dr As OleDbDataReader = myLatestHighestRating.SelectHighestRatingQuiz()

                Dim dt As DataTable = New DataTable()
                dt.Load(dr)


                lstQuizzes.DataSource = dt
                lstQuizzes.DataBind()

            ElseIf (drpSortBy.SelectedValue = 2) And (drpTags.SelectedValue = -1) Then
                Dim myEasiestQuizes As QuizScoreManagement = New QuizScoreManagement()
                Dim dr As OleDbDataReader = myEasiestQuizes.SelectEasyScoreQuiz()

                Dim dt As DataTable = New DataTable()
                dt.Load(dr)


                lstQuizzes.DataSource = dt
                lstQuizzes.DataBind()

            ElseIf (drpSortBy.SelectedValue = 3) And (drpTags.SelectedValue = -1) Then
                Dim myHardestQuizes As QuizScoreManagement = New QuizScoreManagement()
                Dim dr As OleDbDataReader = myHardestQuizes.SelectHardScoreQuiz()

                Dim dt As DataTable = New DataTable()
                dt.Load(dr)


                lstQuizzes.DataSource = dt
                lstQuizzes.DataBind()

            ElseIf (drpSortBy.SelectedValue = 0) And (drpTags.SelectedValue <> -1) Then

                Dim myLatestQuizes As QuizManagement = New QuizManagement()
                Dim dr As OleDbDataReader = myLatestQuizes.SelectQuizzesLatestByTag(Convert.ToInt32(drpTags.SelectedValue))

                Dim dt As DataTable = New DataTable()
                dt.Load(dr)


                lstQuizzes.DataSource = dt
                lstQuizzes.DataBind()

            ElseIf (drpSortBy.SelectedValue = 1) And (drpTags.SelectedValue <> -1) Then

                Dim myLatestHighestRatingTags As Quiz_RatingManagement = New Quiz_RatingManagement()
                Dim dr As OleDbDataReader = myLatestHighestRatingTags.SelectHighestRatingQuizByTagId(Convert.ToInt32(drpTags.SelectedValue))

                Dim dt As DataTable = New DataTable()
                dt.Load(dr)


                lstQuizzes.DataSource = dt
                lstQuizzes.DataBind()

            ElseIf (drpSortBy.SelectedValue = 2) And (drpTags.SelectedValue <> -1) Then

                Dim myEasiestQuizes As QuizScoreManagement = New QuizScoreManagement()
                Dim dr As OleDbDataReader = myEasiestQuizes.SelectEasyScoreQuizByTagId(Convert.ToInt32(drpTags.SelectedValue))

                Dim dt As DataTable = New DataTable()
                dt.Load(dr)


                lstQuizzes.DataSource = dt
                lstQuizzes.DataBind()

            ElseIf (drpSortBy.SelectedValue = 3) And (drpTags.SelectedValue <> -1) Then

                Dim myEasiestQuizes As QuizScoreManagement = New QuizScoreManagement()
                Dim dr As OleDbDataReader = myEasiestQuizes.SelectHardScoreQuizByTagId(Convert.ToInt32(drpTags.SelectedValue))

                Dim dt As DataTable = New DataTable()
                dt.Load(dr)


                lstQuizzes.DataSource = dt
                lstQuizzes.DataBind()

            End If

        Catch ex As Exception

        End Try

    End Sub


    Protected Function getStars(ByVal quizId As Int32)
        Try

            Dim getRating As Quiz_RatingManagement = New Quiz_RatingManagement()
            Dim quizRating = getRating.SelectAVGQuizRatingByQuizId(quizId)

            Dim stars As String = ""

            For index = 0 To quizRating - 1
                stars = stars + "<img alt="""" class=""style6"" src=""CSS/images/FilledStar.png"" />"
            Next
            If (quizRating = 0) Then quizRating = 1
            For index = quizRating To 5
                stars = stars + "<img alt="""" class=""style6"" src=""CSS/images/EmptyStar.png"" />"
            Next

            Return stars

        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Protected Sub lstQuizzes_PagePropertiesChanged(sender As Object, e As EventArgs) Handles lstQuizzes.PagePropertiesChanged
        Try

            listViewGetData()
        Catch ex As Exception

        End Try
    End Sub
End Class